package com.example.es.api;

import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequestBuilder;
import org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequestBuilder;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequestBuilder;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequestBuilder;
import org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsResponse;
import org.elasticsearch.client.AdminClient;
import org.elasticsearch.client.Requests;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.collect.ImmutableOpenMap;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;

import java.util.Map;

public class EsAdmin {
    AdminClient adminClient = null;
    public EsAdmin(   AdminClient adminClient ){
        this.adminClient = adminClient;
    }

    public boolean createIndex(String indexName){
        CreateIndexResponse createIndexResponse = adminClient.indices().prepareCreate(indexName).get();
        return createIndexResponse.isAcknowledged();
    }
    public boolean updateIndexSetting(String index,Map settings){
        UpdateSettingsRequestBuilder updateSettingsRequestBuilder = adminClient.indices().prepareUpdateSettings(index);
        UpdateSettingsResponse updateSettingsResponse = updateSettingsRequestBuilder.setSettings(settings).get();
        return updateSettingsResponse.isAcknowledged();
    }
    public Map getIndexSetttings(String index ){
        GetSettingsRequestBuilder getSettingsRequestBuilder = adminClient.indices().prepareGetSettings(index);
        GetSettingsResponse getSettingsResponse =  getSettingsRequestBuilder.get();
        ImmutableOpenMap<String, Settings> immutableOpenMap =  getSettingsResponse.getIndexToSettings();
        return immutableOpenMap.get(index).getAsMap();
    }

    public boolean createTypeAndMapping(String index,String type,Map fieldProperties){
        PutMappingRequestBuilder putMappingRequestBuilder = adminClient.indices().preparePutMapping(index);
        PutMappingResponse putMappingResponse = putMappingRequestBuilder.setType(type).setSource(fieldProperties).get();
        return putMappingResponse.isAcknowledged();
    }

    public MappingMetaData getTypeMapping(String index,String type){
        GetMappingsRequestBuilder getMappingsRequestBuilder = adminClient.indices().prepareGetMappings(index);
        GetMappingsResponse getMappingsResponse = getMappingsRequestBuilder.setTypes(type).get();
        MappingMetaData mappingMetaData  = getMappingsResponse.getMappings().get(index).get(type);
        return mappingMetaData;
    }

}
